//
//  cinemaViewController.m
//  项目一
//
//  Created by nick on 15/7/17.
//  Copyright (c) 2015年 nick. All rights reserved.
//

#import "cinemaViewController.h"
#define sheight [UIScreen mainScreen].bounds.size.height
#define swidth [UIScreen mainScreen].bounds.size.width
#define sNavHeight 64
#define sTabBarHeight 49
#import "DataService.h"
#import "cinemaModal.h"
#import "cinemaCell.h"
@interface cinemaViewController (){
    NSMutableArray *dataArray;
    NSMutableDictionary *nameAndId;
    BOOL isOpen[150];
    NSArray *groupArray;
    NSMutableDictionary *iteamsAndId;
    UITableView *tv;
}

@end

@implementation cinemaViewController
#pragma mark - tool
- (void)_Grouped{
    iteamsAndId = [NSMutableDictionary new];
    groupArray = [nameAndId allKeys];
    for (NSString *str in groupArray)
    {
        NSMutableArray *items = [NSMutableArray new];
        for (int i = 0; i < dataArray.count;i++)
        {
            cinemaModal *modal = dataArray[i];
            if ([modal.districtId isEqualToString:str])
            {
                [items addObject:modal];

            }
                    }
        [iteamsAndId setValue:items forKey:str];
    }
}
- (void)_dataLoad{
    dataArray = [NSMutableArray new];
    NSDictionary *dic = [DataService setDataWithJason:@"cinema_list"];
    NSArray *cinemaList = [dic objectForKey:@"cinemaList"];
    for (NSDictionary *dic1 in cinemaList) {
        cinemaModal *cinemaData = [cinemaModal new];
        [cinemaData setValuesForKeysWithDictionary:dic1];
        [dataArray addObject:cinemaData];
    }
    
}
- (void)_loadDistrict{
    NSDictionary *dic = [DataService setDataWithJason:@"district_list"];
    NSArray *districtList = [dic objectForKey:@"districtList"];
    nameAndId = [NSMutableDictionary new];
    for (NSDictionary *dic1 in districtList) {
        NSString *id1 = [dic1 objectForKey:@"id"];
        NSString *name = [dic1 objectForKey:@"name"];
        [nameAndId setValue:name forKey:id1];
        
    }
}
#pragma mark - dataSourceAndDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return groupArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSArray *array = [iteamsAndId valueForKey:groupArray[section]];
    if (isOpen[section] == NO) {
        return 0;
    }
    return array.count;
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    cinemaCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    NSArray *array = [iteamsAndId valueForKey:groupArray[indexPath.section]];
    cell.modal = array[indexPath.row];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    NSString *groupName = [nameAndId valueForKey:groupArray[section]];
    UIButton *headBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 0, 30)];
    [headBtn setBackgroundImage:[UIImage imageNamed:@"btnbg_blue.png"] forState:UIControlStateNormal];
    [headBtn setTitle:groupName forState:UIControlStateNormal];
    [headBtn addTarget:self action:@selector(headBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    headBtn.tag = section;
    [headBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    return headBtn;
}
- (void)headBtnAction:(UIButton*)btn{
    isOpen[btn.tag] = !isOpen[btn.tag];
    NSIndexSet *index = [[NSIndexSet alloc]initWithIndex:btn.tag];
    [tv reloadSections:index withRowAnimation:UITableViewRowAnimationFade];
    
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 30;
}
#pragma mark - createSubview
- (void)_createTableView{
    tv = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, swidth, sheight-sNavHeight-sTabBarHeight)style:UITableViewStylePlain];
    tv.delegate = self;
    tv.dataSource = self;
    [self.view addSubview:tv];
    tv.backgroundColor = [UIColor blackColor];
    
    [tv registerNib:[UINib nibWithNibName:@"cinemaCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"cell"];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self _dataLoad];
    [self _loadDistrict];
    [self _Grouped];
    [self _createTableView];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
